package com.top.horizon.th1804app.ui.fragment;

import android.view.View;
import android.widget.TextView;

import com.net.protocol.model.DeviceInfoModel;
import com.top.horizon.th1804app.R;
import com.top.horizon.th1804app.base.BaseFragment;
import com.top.horizon.th1804app.view.PieData;
import com.top.horizon.th1804app.view.PieView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class DeviceDetailFragment extends BaseFragment {

    private ArrayList<PieData> dataList;
    private TextView tvMenorySizeAll;
    private TextView tvTemperature;
    private PieView pieView;
    //设备温度
    public double temp;
    //设备总容量 M
    public int totalProtein;
    //设备可用
    public int availableProtein;
    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_device_detail;
    }

    @Override
    protected void initView() {
        pieView = rootView.findViewById(R.id.pie_view);
        tvMenorySizeAll = rootView.findViewById(R.id.tv_memory_size_all);
        tvTemperature = rootView.findViewById(R.id.tv_temperature);
    }

    @Override
    protected void initData() {
    //此处请求服务器的数据，并显示温度，内存大小，已用内存大小
        dataList = new ArrayList<>();
        dataList.add(new PieData("可用",220));
        dataList.add(new PieData("已用",20));
        pieView.setData(dataList);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void heartEvent(DeviceInfoModel deviceInfoModel){
        totalProtein = deviceInfoModel.totalProtein / 1024;
        tvMenorySizeAll.setText(String.valueOf(totalProtein)+"G");
        tvTemperature.setText(String.valueOf(deviceInfoModel.temp)+"℃");
        availableProtein = deviceInfoModel.availableProtein / 1024;
        dataList = new ArrayList<>();
        dataList.add(new PieData("已用",totalProtein-availableProtein));
        dataList.add(new PieData("可用",availableProtein));

        pieView.setData(dataList);
    }
}
